package game.module.mj.processor;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.mj.bean.MjRound;
import game.module.mj.logic.MjDataManager;
import game.session.SessionManager;
import lion.common.ArrayUtility;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.C2SMJYaoshaizi;
import lua.LuaMessageMj.PushMJYaoshaizi;
import lua.LuaMessageMj.S2CMJYaoshaizi;

@MsgCodeAnn(msgcode = C2SMJYaoshaizi.id, accessLimit = 500)
public class MjYaoshaiziProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(MjYaoshaiziProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw request) throws Exception {
		// TODO Auto-generated method stub
		/*
		C2SMJYaoshaizi reqMsg = C2SMJYaoshaizi.parse(request);
		int playerId = playingRole.getId();
		logger.info("yao shaizi,playerId={},call_id={}", playerId);
		// 房间是否存在
		MjRound mjRound = MjDataManager.getInstance().getMjRoom(playerId);
		if (mjRound == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		Set<Integer> enter_players = mjRound.getEnter_player();
		if(enter_players.size()<4){
			S2ErrorCode respMsg = new S2ErrorCode(502);//还有玩家未进入房间
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		if (playingRole.getId() != mjRound.getZhuangjia_id()) {
			S2ErrorCode respMsg = new S2ErrorCode(501);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;// 如果玩家的id是庄家
		}
		/*
		short[] Shaizi_num = new short[2];
		Shaizi_num[0] = (short) RandomUtils.nextInt(1, 7);
		Shaizi_num[1] = (short) RandomUtils.nextInt(1, 7);
		PushMJYaoshaizi PushMJYaoshaizi = new PushMJYaoshaizi();
		PushMJYaoshaizi.Shaizi_num = Shaizi_num;
		List<Integer> playerIds = mjRound.getPlayerIds();
		for (int i = 0; i < playerIds.size(); i++) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(playerIds.get(i));
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(PushMJYaoshaizi.build(pr.alloc()));
			}
		}
		*/
		/*
		S2CMJYaoshaizi S2CMJYaoshaizi = new S2CMJYaoshaizi();
		playingRole.writeAndFlush(S2CMJYaoshaizi.build(playingRole.alloc()));*/
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
